package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.Absence;
import com.dj.dormitory.entity.Student;
import com.dj.dormitory.entity.req.AbsenceAddReq;
import com.dj.dormitory.entity.req.AbsenceReq;
import com.dj.dormitory.service.AbsenceService;

import com.dj.dormitory.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/absence")
public class AbsenceController {

    @Autowired
    private AbsenceService absenceService;

    @Autowired
    private StudentService studentService;

    @PostMapping("getList")
    public Result getList(@RequestBody Map map, HttpServletRequest request){
        if((Integer) request.getAttribute("type")==2){
            //获得sno
            Integer studentId = (Integer) request.getAttribute("studentId");
            Student stu = studentService.getById(studentId);
            map.put("sno",stu.getSno());
        }
        Page<Absence> page = absenceService.getList(map);
        return Result.ok().data("records",page.getRecords()).data("count",page.getTotal());
    }

    @PostMapping("add")
    public Result add(@RequestBody AbsenceAddReq absenceAddReq) {
        Absence absence = new Absence();
        BeanUtils.copyProperties(absenceAddReq,absence);
        absenceService.save(absence);
        return Result.ok().message("添加缺勤成功");
    }
    @PostMapping("edit")
    public Result edit(@RequestBody Absence absence){
        absenceService.updateById(absence);
        return Result.ok().message("编辑缺勤成功");
    }
    @DeleteMapping("deleteOne/{id}")
    public Result deleteOne(@PathVariable String id){
        absenceService.removeById(id);
        return Result.ok().message("删除缺勤成功");
    }
    @DeleteMapping("deletePart/{ids}")
    public Result deletePart(@PathVariable("ids") List<String> ids){
        absenceService.removeByIds(ids);
        return Result.ok().message("批量删除缺勤成功");
    }

}

